package cn.com.generaldata.os.service.indicator_evaluation.coefficient_of_variation;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 区位熵变异系数评估指标计算类.
 */
public class LocationEntropyVariatCalculateService extends IndicatorEvaluationCalculateService {

	private String[] cityAreas = ProvinceIndicatorConstant.CITYS;
	private String GOPArea = ProvinceIndicatorConstant.GOP_INDICATOR_NAME;
	private String GDPArea = ProvinceIndicatorConstant.GDP_INDICATOR_NAME;
	private String JsPROVICE = ProvinceIndicatorConstant.PROVICE_AREA_ID;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		IndicatorAccountVO iav = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		IndicatorAccountVO iavJS = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		iavJS.setAreaId(JsPROVICE);
		//江苏省GOP
		Double gopProvice = getInputOrAccountingIndicatorValue(iavJS, indicatorAccountVO.getYear(), GOPArea);
		Validate.isTrue(gopProvice != null);
		//江苏省GDP
		Double gdpProvice = getInputOrAccountingIndicatorValue(iavJS, indicatorAccountVO.getYear(), GDPArea);
		Validate.isTrue(gdpProvice != null);
		Double ySum = 0d;
		Map cityLEMap = new HashMap();
		for (String city : cityAreas) {
			iav.setAreaId(city);
			//城市GOP
			Double gopCity = getInputOrAccountingIndicatorValue(iav, indicatorAccountVO.getYear(), GOPArea);
			Validate.isTrue(gopCity != null);
			//城市GDP
			Double gdpCity = getInputOrAccountingIndicatorValue(iav, indicatorAccountVO.getYear(), GDPArea);
			Validate.isTrue(gdpCity != null);
			//城市海洋经济区位熵
			Double cityLocationEntropy = (gopCity / gdpCity)/(gopProvice/gdpProvice);
			cityLEMap.put(city, cityLocationEntropy);
			ySum += cityLocationEntropy;
		}
		Double yAverage = ySum / 3;
		Double sum = 0d;
		for (String city : cityAreas) {
			Double cityLocationEntropy = (Double) cityLEMap.get(city);
			sum += Math.pow((cityLocationEntropy - yAverage), 2);
		}
		return Math.sqrt(sum / 3) / yAverage;
	}

}
